//
//  YCLoginViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/1.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCLoginViewController.h"
#define LOGO_WIDTH S(120)
#define LOGO_HEIGHT S(120)

#define ORGIN_X S(25)
#define kMarginW 75/2
#define FIELD_HEIGHT S(65)
@interface YCLoginViewController ()
{
    UITextField *m_phoneField;
    UITextField *m_passwordField;
    
    
    UIImageView *m_logoView;
    UIButton *m_btnLogin;
    UIButton *m_btnForget;
    CGFloat _maxY;
    CGFloat _centerY;
    
    UITextField *m_tintField;
}
@end

@implementation YCLoginViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:@"keyboardShow" object:nil];
    [self addMainView];
}
-(void)addMainView
{
    [self.view setBackgroundColor:MAIN_COLOR];
    
    CGFloat logoX=(kMainScreen_Width-LOGO_WIDTH)*0.5;
    m_logoView=[[UIImageView alloc]initWithFrame:CGRectMake(logoX, S(100), LOGO_WIDTH, LOGO_HEIGHT)];
    [m_logoView setImage:[UIImage imageNamed:@"login_logo"]];
    [self.view addSubview:m_logoView];
    _centerY=m_logoView.center.y;
    
    UIView *backView=[[UIView alloc]init];
    [backView setBackgroundColor:WHITE_COLOR];
    backView.layer.cornerRadius=2.0f;
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kMarginW);
        make.right.equalTo(self.view).offset(-kMarginW);
        make.top.equalTo(self.view).offset(S(350));
        make.height.mas_equalTo(S(130));
    }];
    CGFloat width=kMainScreen_Width-2*kMarginW;
    [backView.layer addSublayer:getLine(ORGIN_X, width-ORGIN_X, S(65), S(65), LINE_COLOR)];
    
    m_tintField=[[UITextField alloc]init];
    [m_tintField setBackgroundColor:WHITE_COLOR];
    [m_tintField setFont:[UIFont LightFontOfSize:14]];
    [m_tintField setTextColor:MAIN_COLOR];
    [self.view addSubview:m_tintField];
    [m_tintField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kMarginW);
        make.right.equalTo(self.view).offset(-kMarginW);
        make.top.equalTo(backView).offset(-35);
        make.height.mas_equalTo(30);
    }];
    m_tintField.layer.cornerRadius=2.0f;
    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 30)];
    m_tintField.leftView=leftView;
    m_tintField.leftViewMode=UITextFieldViewModeAlways;
    m_tintField.enabled=NO;
    [m_tintField setHidden:YES];
    
    CGFloat fieldW=width-2*ORGIN_X;
    m_phoneField=[[UITextField alloc]initWithFrame:CGRectMake(ORGIN_X, 0, fieldW, FIELD_HEIGHT)];
    [m_phoneField setFont:[UIFont LightFontOfSize:17]];
    [m_phoneField setPlaceholder:@"请输入您的手机号" Corlor:DISABLE_COLOR];
    m_phoneField.delegate=self;
    m_phoneField.keyboardType=UIKeyboardTypeNumberPad;
    m_phoneField.clearButtonMode=UITextFieldViewModeWhileEditing;
    [backView addSubview:m_phoneField];
    
    m_passwordField=[[UITextField alloc]initWithFrame:CGRectMake(ORGIN_X, CGRectGetMaxY(m_phoneField.frame), fieldW, FIELD_HEIGHT)];
    [m_passwordField setFont:[UIFont LightFontOfSize:17]];
    [m_passwordField setPlaceholder:@"请输入密码" Corlor:DISABLE_COLOR];
    m_passwordField.delegate=self;
    m_passwordField.secureTextEntry=YES;
    m_passwordField.clearButtonMode=UITextFieldViewModeWhileEditing;
    m_passwordField.keyboardType = UIKeyboardTypeASCIICapable;
    [backView addSubview:m_passwordField];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_phoneField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_passwordField];
    
    m_btnLogin=[[UIButton alloc]init];
    [m_btnLogin.titleLabel setFont:[UIFont LightFontOfSize:18]];
    [m_btnLogin setBackgroundColor:WHITE_COLOR];
    [m_btnLogin setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [m_btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    m_btnLogin.layer.cornerRadius=2.0f;
    [m_btnLogin addTarget:self action:@selector(loginBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m_btnLogin];
    [m_btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kMarginW);
        make.right.equalTo(self.view).offset(-kMarginW);
        make.top.equalTo(backView.bottom).offset(S(40));
        make.height.mas_equalTo(50);
    }];
    [self addKeyboardNote:S(390)+180];
    _maxY=S(390)+180;
    
    m_btnForget=[[UIButton alloc]init];
    [m_btnForget setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [m_btnForget.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [m_btnForget.titleLabel setTextAlignment:NSTextAlignmentRight];
    [m_btnForget setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.view addSubview:m_btnForget];
    [m_btnForget addTarget:self action:@selector(forgetPasswordBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kMarginW);
        make.top.equalTo(m_btnLogin.bottom).offset(0);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(40);
    }];
    
}
-(void)loginBtnClicked:(UIButton *)button
{
    if ([m_phoneField.text isMobile]) {
        button.enabled=NO;
        UserLoginRequest *request = [UserLoginRequest new];
        request.phone = m_phoneField.text;
        request.password = m_passwordField.text;
        [apiClient hideProgress];
        [apiClient disableAfterRequest];
        [apiClient doUserLogin:request success:^(ApiResponse *data, NSString *url) {
            UserLoginResponse *response=[[UserLoginResponse alloc]initWithCGResponse:data];
            [App shared].user = response.data;
            [[App shared] save];
            [[App shared] restore];
            
            button.enabled=NO;
            [self.view showHudWith:data.message];            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[AppDelegate appDelegate]loadLogin];
            });
            
        }failure:^(ApiResponse *data, NSString *url){
            button.enabled=NO;
            [m_tintField setHidden:NO];
            [m_tintField setText:data.message];
        }];
    }else {
        [self.view showHUD:@"请输入正确的手机号码"];
    }

    
}
-(void)forgetPasswordBtnClicked:(UIButton *)button
{
    YCRegisterViewController *registerVC=[[YCRegisterViewController alloc]initWith:YES];
    [self showNavigationView:registerVC];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField==m_phoneField) {
        if (range.location>10) {
            return NO;
        }
        return YES;
    }
    return YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [m_phoneField resignFirstResponder];
    [m_passwordField resignFirstResponder];
}
- (void)addKeyboardNote:(CGFloat)maxY
{
    _maxY=maxY;
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note
{
    CGFloat keyboardH = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGFloat keyboardY = self.view.frame.size.height - keyboardH;
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    if (duration <= 0.0) {
        duration = 0.25;
    }
   
    [UIView animateWithDuration:duration animations:^{
        if (_maxY > keyboardY) {
            self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - _maxY - 5);
            
            m_logoView.transform = CGAffineTransformMakeScale(0.5,0.5);
            m_logoView.center=CGPointMake(m_logoView.center.x, _centerY+100);
        } else {
            self.view.transform = CGAffineTransformIdentity;
            
            m_logoView.transform = CGAffineTransformIdentity;
        }
    }];
}
- (void)keyboardWillHide:(NSNotification *)note
{
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.view.transform = CGAffineTransformIdentity;
        m_logoView.transform = CGAffineTransformIdentity;
        m_logoView.center=CGPointMake(m_logoView.center.x, _centerY);
    }];
}
-(void)keyboardShow:(id)sender
{
    [m_phoneField resignFirstResponder];
    [m_passwordField resignFirstResponder];
}
-(void)textChange
{
    [m_tintField setHidden:YES];
    m_btnLogin.enabled= (m_phoneField.text.length==11);
}



@end
